package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.domain.AssignRecord;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.service.IAssignRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.BusinessMapper;
import com.ruoyi.crm.domain.Business;
import com.ruoyi.crm.service.IBusinessService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 商机Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-13
 */
@Service
public class BusinessServiceImpl implements IBusinessService 
{
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private IAssignRecordService assignRecordService;
    /**
     * 查询商机
     * 
     * @param id 商机主键
     * @return 商机
     */
    @Override
    public Business selectBusinessById(Long id)
    {
        return businessMapper.selectBusinessById(id);
    }

    /**
     * 查询商机列表
     * 
     * @param business 商机
     * @return 商机
     */
    @Override
    public List<Business> selectBusinessList(Business business)
    {
        return businessMapper.selectBusinessList(business);
    }

    /**
     * 新增商机
     * 
     * @param business 商机
     * @return 结果
     */
    @Override
    public int insertBusiness(Business business)
    {
        business.setCreateTime(DateUtils.getNowDate());
        return businessMapper.insertBusiness(business);
    }

    /**
     * 修改商机
     * 
     * @param business 商机
     * @return 结果
     */
    @Override
    public int updateBusiness(Business business)
    {
        business.setUpdateBy(SecurityUtils.getUsername());
        business.setUpdateTime(DateUtils.getNowDate());
        return businessMapper.updateBusiness(business);
    }

    /**
     * 批量删除商机
     * 
     * @param ids 需要删除的商机主键
     * @return 结果
     */
    @Override
    public int deleteBusinessByIds(Long[] ids)
    {
        return businessMapper.deleteBusinessByIds(ids);
    }

    /**
     * 删除商机信息
     * 
     * @param id 商机主键
     * @return 结果
     */
    @Override
    public int deleteBusinessById(Long id)
    {
        return businessMapper.deleteBusinessById(id);
    }

    @Override
    @Transactional
    public int giveBack(Long id) {
        AssignRecord assignRecord=new AssignRecord();
        assignRecord.setId(id);
        assignRecord.setType(AssignRecord.Type.BUSINESS.getValue());
        assignRecord.setLatest(AssignRecord.LatestStatus.STALE.getValue());
        assignRecordService.updateAssignRecord(assignRecord);
        Business business=businessMapper.selectBusinessById(id);
        business.setStatus(Clue.Status.FAKE.getValue());
        return businessMapper.updateBusiness(business);
    }
}
